#!/bin/bash

set -e

if [ -z "$1" ]; then
    echo "No rsp file specified"
    exit 1
elif [ ! -f $1 ]; then
	echo "Can't find RSP file $1"
	exit 1
fi

cat /install/limits.txt >> /etc/security/limits.conf

groupadd --gid 1001 dba && useradd -m -G dba --uid 1001 oracle
mkdir /u01 && chown oracle:dba /u01 && chmod 775 /u01

#Download oracle database zip
echo "Downloading oracle database zip"
wget -q -O /oracle_database1.zip http://192.168.1.115:90/linuxamd64_12102_database_1of2.zip
wget -q -O /oracle_database2.zip http://192.168.1.115:90/linuxamd64_12102_database_2of2.zip

echo "Extracting oracle database zip"
su oracle -c 'unzip -q /oracle_database1.zip -d /home/oracle/' && rm -f /oracle_database1.zip
su oracle -c 'unzip -q /oracle_database2.zip -d /home/oracle/' && rm -f /oracle_database2.zip

#Run installer
su oracle -c "cd /home/oracle/database && ./runInstaller -silent -ignorePrereq -ignoreSysPrereqs -responseFile $1 -waitForCompletion"
#Cleanup
echo "Cleaning up"
rm -rf /home/oracle/database /tmp/*

#Move product to custom location
mv /u01/app/oracle/product /u01/app/oracle-product

#Run Oracle root scripts
/u01/app/oraInventory/orainstRoot.sh > /dev/null 2>&1
echo | /u01/app/oracle/product/12.1.0/db1/root.sh > /dev/null 2>&1 || true
